package com.jxwifi.cloud.quickclean.toolkit;

import android.content.Context;
import android.support.v7.widget.LinearLayoutManager;
import android.widget.ImageView;

import com.alibaba.fastjson.JSON;
import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.jxwifi.cloud.quickclean.R;
import com.jxwifi.cloud.quickclean.adapter.LogisticsAdapter;
import com.jxwifi.cloud.quickclean.adapter.TkDetailsAdapter;
import com.jxwifi.cloud.quickclean.app.UrlPool;
import com.jxwifi.cloud.quickclean.bean.AliLogisticsInfoResultBean;
import com.jxwifi.cloud.quickclean.model.AuzImpl;
import com.jxwifi.cloud.quickclean.okhttp.Params;

public class LogisticsPresenter {

    private Context mContext;
    private AuzImpl mAuz;
    private LogisticsView mView;

    private LogisticsAdapter mAdapter;

    public LogisticsPresenter(Context context, LogisticsView view) {
        this.mContext = context;
        this.mView = view;
        mAuz = new AuzImpl();
    }

    public void initData() {
        Params params = new Params();
        params.add("distributionNo", mView.getDistributionNo());
        params.add("type", mView.getTravallingType());
        mAuz.auzget(UrlPool.GET_QUERY_BY_NO_AND_TYPE, params, new AuzImpl.AuzListener() {
            @Override
            public void AuzSuccess(int code, String data) {
                if (code == 0) {
                    AliLogisticsInfoResultBean mBean = JSON.parseObject(data, AliLogisticsInfoResultBean.class);
                    initView(mBean);
                } else {
                    mView.oninitData();
                }
            }

            @Override
            public void AuzFailure(int errorCode, String msg) {
                mView.onFailure(errorCode, msg);
            }
        });
    }

    public void initView(AliLogisticsInfoResultBean mBean) {

        mView.getLogisticsCompany().setText(mBean.getResult().getExpName());
        mView.getStartsOrder().setText(mView.getDistributionNo());
        Glide.with(mContext).load(mBean.getResult().getLogo()).error(R.mipmap.icon_default_head).into(mView.getLogisticsImages());

        mView.getRvList().setLayoutManager(new LinearLayoutManager(mContext));
        if (mBean.getResult().getList().size() > 0) {
            mView.getRvList().setAdapter(mAdapter = new LogisticsAdapter(R.layout.list_logistics_item, mBean.getResult().getList(), mBean.getResult().getList().get(0).getTime()));

        } else {
            mView.getRvList().setAdapter(mAdapter = new LogisticsAdapter(R.layout.list_logistics_item, mBean.getResult().getList(), ""));
        }
        mAdapter.openLoadAnimation();
        mAdapter.openLoadAnimation(BaseQuickAdapter.FOOTER_VIEW);

    }
}
